/**
 * Created by zxh on 14-7-14.
 */

ms.UIYaoWu = ms.UILayer.extend({

    initLayer: function() {
        this._ccsFileName = "yaowu.json";
        this._super();
        this._image.setScale(0.9);

        return true;
    },

    onContinueButtonTouchEnded: function() {
        cc.director.runScene(ms.createScene(ms.UISummary));
    },

    onTaolunButtonTouchEnded: function() {
        var layer = ms.createLayer(ms.UITextLayer);
        layer.setText("        该患者共病焦虑障碍，且抑郁症状反复发作，症状逐渐加重，治疗难度加大，治疗周期延长。社会功能受损严重，患者的记忆力、反应能力等功能难以恢复到病前状态\n\n\
        抗抑郁药种类很多，且各有特点，在制定抗抑郁治疗方案时应根据患者的症状特点进行个体化选药，兼顾有效、安全及耐受性，才能取得更显著的治疗效果。对于本例患者，在选择抗抑郁药物时，除了考虑能同时有效治疗抑郁及焦虑症状外，还应兼顾以下因素：①要能改善患者失眠症状；②考虑到患者既往依从性差，可考虑起效快、早期疗效肯定的药物，让患者快速缓解症状，从而增强治疗的信心。2010年发表的一项研究观察了早期改善的患者和早期未改善的患者最终出院时的治疗结果，发现在早期改善的患者中，出院时的有效率达到了80%，远高于早期未改善的患者。所以，药物的快速起效，能够使患者早期改善，能够预测远期疗效。");
        var winSize = cc.director.getWinSize();
        layer.x += layer.width;
        this.addChild(layer);
//
        var move = cc.MoveTo.create(0.3, cc.p(winSize.width * 0.1, 0));
        layer.runAction(move);
    },

    onAddButtonTouchEnded: function() {
        if (this.getChildByTag(123)) {
            return;
        }

        var layer = ms.createLayer(ms.UIYaoWu2);
        layer.x = this.width * 0.805;
        layer.y = -38;
        layer.setYaowuLayer(this);
        this.addChild(layer, 0, 123);

        var move = cc.MoveBy.create(0.3, cc.p(-190, 0));
        this._image.runAction(move);
        layer.runAction(move.clone());
    },

    onYaoWu2Close: function() {
        var move = cc.MoveBy.create(0.3, cc.p(190, 0));
        this._image.runAction(move);
    },

    addItem: function(item) {
        var item = ms.ListItem.create(item, this._listView);
        //this._listView.pushBackCustomItem(item);
    }
});

ms.ListItem = cc.Class.extend({
    load: function(item, listView) {

        var root = ccs.uiReader.widgetFromJsonFile("res/ccs/listItem.json")

        root.getChildByName("_textName").string = item.name;
        root.getChildByName("_textNum").string = item.num;
        root.getChildByName("_textUse").string = item.use;
        root.getChildByName("_textTime").string = item.time;

        listView.pushBackCustomItem(root);
        root.getChildByName("_delButton").addTouchEventListener(function(sender, type) {
            if (type === 2) {
                var index = listView.getIndex(root);
                listView.removeItem(index);
            }
        }, root);

        return root;
    }
});

ms.ListItem.create = function(item, listView) {
    var temp = new ms.ListItem();
    return temp.load(item, listView);
};

ms.UIYaoWu2 =  ms.UILayer.extend({
    _yaowuLayer: null,

    ctor: function() {
        this._super();
        this._ccsFileName = "yaowu2.json";
    },

    setYaowuLayer: function(yaowuLayer) {
        this._yaowuLayer = yaowuLayer;
    },

    initLayer: function() {
        this._super();
        this.setContentSize(this.rootNode.getSize());
        var config = cc.loader.getRes("res/ccs/config.json");

        //药名
        var combobox = ms.Combobox.create(cc.size(200, 100));
        combobox.setPosition(cc.p(100, this.height * 0.729));
        combobox.addItems(config.yaowu);
        this.addChild(combobox);
        this._comboboxName = combobox;

        //计量
        var comboboxNum = ms.Combobox.create(cc.size(80, 50));
        this.addChild(comboboxNum);
        comboboxNum.addItems(["mg", "片"]);
        comboboxNum.x = this.width * 0.75;
        comboboxNum.y = this.height * 0.24;
        this._comboboxNum = comboboxNum;

        //每天计量
        var comboboxDay = ms.Combobox.create(cc.size(80, 50));
        this.addChild(comboboxDay);
        comboboxDay.addItems(["mg", "片"]);
        comboboxDay.x = this.width * 0.75;
        comboboxDay.y = this.height * 0.18;
        this._comboboxDay = comboboxDay;

        //疗程
        var comboboxTime = ms.Combobox.create(cc.size(80, 50));
        this.addChild(comboboxTime);
        comboboxTime.addItems(["天", "周", "月", "年"]);
        comboboxTime.x = this.width * 0.75;
        comboboxTime.y = this.height * 0.12;
        this._comboboxTime = comboboxTime;

        //用法
        var comboboxUse = ms.Combobox.create(cc.size(80, 50));
        this.addChild(comboboxUse);
        comboboxUse.addItems(["每天", "每周", "每小时"]);
        comboboxUse.x = this.width * 0.25;
        comboboxUse.y = this.height * 0.18;
        this._comboboxUse = comboboxUse;
        return true;
    },

    onCloseButtonTouchEnded: function() {
        this._yaowuLayer.onYaoWu2Close();
        var move = cc.MoveBy.create(0.5, cc.p(500, 0));
        var call = cc.CallFunc.create(function() {
            this.removeFromParent();
        }, this);
        this.runAction(cc.Sequence.create(move, call));
    },

    onButtonOkTouchEnded: function() {
        if (this._yaowuLayer) {
            var item = {};
            item["name"] = this._comboboxName.getString();
            item["num"]  = this._numText.string + this._comboboxUse.getString();
            item["use"]  = this._useText.string + this._comboboxDay.getString();
            item["time"] = this._timeText.string + this._comboboxTime.getString();
            //item["useParam1"] = this._useText.string;
            //item["useParam2"] = this._comboboxDay.getString();

            this._yaowuLayer.addItem(item);
        }
    }
});


ms.Combobox = ms.UILayer.extend({
    _list: null,
    _selector: null,
    _target: null,
    _stringItems: null,
    _y: null,
    initLayer: function(size) {
        this._ccsFileName = "combobox.json";
        this._super();
        this._size = size;
        this._labelText.string = "";
        //this.setContentSize(size);
        //this.rootNode.setSize(size);
        this.setLocalZOrder(1);
        this._labelText.setTouchEnabled(false);
        this._imageBtn.width = size.width;
        //this._imageBtn.setLocalZOrder();
        //this._listView.ignoreAnchor = false;
        //this._listView.setAnchorPoint(cc.p(0, 1));
        this._listView.setSize(size);
        var y = (this._imageBtn.y - this._imageBtn.height) - this._listView.height;
        this._listView.y = y;//(this._imageBtn.y - this._imageBtn.height) - this._listView.height;
        this._listView.setBounceEnabled(true);
        this._listView.setTouchEnabled(true);
        this._listView.setBounceEnabled(true);
        this._listView.addEventListenerListView(this.selectedItemEvent, this);

        this._stringItems = [];
        this._listView.setVisible(false);
        return true;
    },

    onImageBtnTouchEnded: function() {
        this._listView.setVisible(!this._listView.isVisible());
        this._listView.getInnerContainer().x = 0;
        if (this._listView.isVisible()) {
            this.setLocalZOrder(10);
        } else {
            this.setLocalZOrder(0);
            this._listView.setTouchEnabled(false);
            this._listView.setTouchEnabled(true);
        }

    },

    setSelectedEvent: function(selector, target) {
        this._selector = selector;
        this._target = target;
    },

    selectedItemEvent: function (sender, type) {
        if (!sender.isVisible()) {
            return;
        }
        switch (type) {
            case ccui.ListView.EVENT_SELECTED_ITEM:
                var listViewEx = sender;
                var index = listViewEx.getCurSelectedIndex();
                if (this._selector && this._target) {
                    this._selector.call(this._target, this._stringItems[index]);
                }

                this._labelText.string = this._stringItems[index];
                listViewEx.setVisible(false);
                break;

            default:
                break;
        }
    },

    onEnter: function() {
        this._super();
        if (this._stringItems.length) {
            this._labelText.string = this._stringItems[0];
        }
    },


    addItem: function(string) {

        this._stringItems.push(string);
        var text = ccui.Text.create();
        text.string = string;
        text.setColor(cc.color.BLACK);
        text.setTouchEnabled(true);
        this._listView.pushBackCustomItem(text);
    },

    addItems: function(strings) {
        for(var i = 0; i < strings.length; i++) {
            this.addItem(strings[i]);
        }
    },

    getString: function() {
        return this._labelText.string;
    }

});

ms.Combobox.create = function(size) {
    var ret = new ms.Combobox();
    if (ret && ret.initLayer(size)) {
        return ret;
    }
};